<?php
class AMS_Core_Model_Core_Tree extends AMS_Core_Model_Abstract {
    protected $items;
    const CHILDREN = '__children_items__';
    
    protected function __createPath($path){
        $current = &$this->items;
        foreach (explode('/',trim($path,'/')) as $index) {
            if (!isset($current[$index]))
                $current[$index] = array();
                $current = &$current[$index];
        }
    }
    
    protected function __getPath($path) {
        $current = &$this->items;
        foreach (explode('/',trim($path,'/')) as $index) {
            if (!isset($current[$index])) return false;
            $current = &$current[$index];
        }
        if (!isset($current[self::CHILDREN]))
            $current[self::CHILDREN] = AMS::getModel('core/collection');
        return $current[self::CHILDREN];
    }
    
    protected function __setDataByPath($path,$value) {
        $collection = $this->__getPath($path);
        $collection->add($value);
    }
    
    protected function __getDataByPath($path,$value) {
        $current = &$this->items;
        foreach (explode('/',trim($path,'/')) as $index) {
            if (!isset($current[$index])) return false;
            $current = &$current[$index];
        }
        return $current[self::CHILDREN];
    }

    public function addItem($item, $path) {
        $this->__createPath($path);
        $this->__setDataByPath($path,$item);
    }
    
    public function getItems($path) {
        return $this->__getPath($path);
    }
}
?>
